#include<iostream>

class Mediator
{
    public:
        virtual void SendMS(const::std::string& msg)=0;
};
class AbstractPlayer//抽象用户
{
public:
    AbstractPlayer(const std::string&name,Mediator *m):name(name),m(m){}
    virtual void SendMsg(const std::string& msg)=0;
    virtual void GetMsg(const std::string&msg)=0;

protected:
    Mediator*m;
    std::string name
};

class SellPerson//卖房子的人
{
public:
    using AbstractPlayer::AbstractPlayer;
    void SendMsg(const std::string&msg)
    {
        m->SendMsg(msg,this);
    }
      void GetMsg(const std::string&msg)
    {
        std::cout<<name<<"卖房子的人收到消息"<<msg<<std::endl;
    }
};

class BuyPerson:public AbstractPlayer
{
public:
    using AbstractPlayer::AbstractPlayer;
    void SendMsg(const std::string&msg)
    {
        m->SendMsg(msg,this);
    }
      void GetMsg(const std::string&msg)
    {
        std::cout<<name<<"买房子的人收到消息"<<msg<<std::endl;
    }
};

class ConcreMediator:public Mediator//具体的中介者
{
public:
    void Setplayer(AbstractPlayer *p1,AbstractPlayer *p2)
    {
        this->p1=p1;
        this->p2=p2;
    }
    void SendMsg(const std::string &msg,AbstractPlayer *p)
    {
        if(p==p1)
        {
            p2->GetMsg(msg);
        }
    }
    private:
        AbstractPlayer *p1;
        AbstractPlayer *p2;
}
int main()
{
    ConcreMediator *m=new ConcreMediator;
    AbstractPlayer *p1=new SellPerson("卖房子1",m);
    AbstractPlayer *p2=new SellPerson("卖房子2",m);
    AbstractPlayer *p3=new SellPerson("买房子1",m);
    AbstractPlayer *p4=new SellPerson("买房子2",m);


    m->Setplayer(p1,p3);
    p1->SendMsg("你好,房子卖了200万");//将本要需要通信联系的两个类进行解耦




    return 0;
}